package com.blb.demo1;

public class Demo1 {

    private static Object ob1 = new Object();
    private static Object ob2 = new Object();

    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (ob1) {
                    for (int i = 0; i < 50; i++) {
                        System.out.print("a");
                        synchronized (ob2){
                            ob2.notify();
                        }
                        try {
                            ob1.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (ob2) {
                    for (int i = 0; i < 50; i++) {
                        System.out.print("b");
                        synchronized (ob1){
                            ob1.notify();
                        }
                        try {
                            ob2.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }).start();


    }


}
